﻿using System.Windows;
using System.Windows.Media;
using ToServer;
using DrawerGeneral;
using System.Net;
using System;
using System.Text;
using Drawer.Controls;

namespace Drawer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ToServerCommunicator tsc;
        private WrapperTransporterClass wtc;
        private String ServerIP = "localhost";
        private int ServerPort = 6123;

        public MainWindow(String ServerIP, int Port)
        {
            InitializeComponent();
            this.ServerIP = ServerIP;
            this.ServerPort = Port;
            drawToolbar.SelectedShapeChanged += drawCanvas.Toolbar_SelectedShapeChanged;

            drawCanvas.Stroke = Brushes.SteelBlue;
            drawCanvas.Fill = Brushes.LightSteelBlue;            

            // Get current IPHostEntry
            IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());

            // Get client hostname
            StringBuilder clientName = new StringBuilder();
            // Get current DateTime
            clientName.Append(DateTime.Now + " ");
            // Get HostName
            clientName.Append(hostEntry.HostName + " ");
            // Get Ip Address
            foreach (var item in hostEntry.AddressList)
            {
                if (item.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    clientName.Append(item + "\n");
            }
            tsc = new ToServerCommunicator(clientName.ToString(), drawCanvas, ServerIP, ServerPort);
            // Add listener to VisualChildrenChanged (tsc.CanvasChangeHandler)
            drawCanvas.VisualChildrenChanged += tsc.CanvasChangeHandler;
            // Add listener to VisualChildEdited
            drawCanvas.VisualChildEdited += tsc.CanvasEditHandler;
            wtc = new WrapperTransporterClass();
            RegisterMethodsToWrapperTransporterClass();
            tsc.ToServerConn.RegisterWrapperTransporterClass(clientName.ToString(), wtc);
            tsc.ToServerConn.TestWrite(clientName.ToString(), "connect");
        }

        private void RegisterMethodsToWrapperTransporterClass()
        {
            wtc.DelegateSendMessageToClient = ReceiveNotifyFromServerHandler;
            wtc.DelegateAddVisualElement = ReceiveAddVisualElementNotifyHandler;
            wtc.DelegateRemoveVisualElement = ReceiveRemoveVisualElementNotifyHandler;
            wtc.DelegateEditVisualElement = ReceiveEditVisualElementNotifyHandler;
        }

        

        #region From server drawing message handlers

        private void ReceiveNotifyFromServerHandler(String msg)
        {
            MessageBox.Show(msg);
        }

        private void ReceiveAddVisualElementNotifyHandler(CanvasChildrenChangeEventArgs e)
        { 
            //MessageBox.Show("YEAH"); 
            this.drawCanvas.AddChild("SERVER", e.VisualAdded, e.ChangedIndex);
        }

        private void ReceiveRemoveVisualElementNotifyHandler(CanvasChildrenChangeEventArgs e)
        {
            this.drawCanvas.RemoveChild("SERVER", e.VisualRemoved, e.ChangedIndex);
        }

        private void ReceiveEditVisualElementNotifyHandler(CanvasChildEditEventArgs e)
        {
            this.drawCanvas.UpdateChild("SERVER", e.UpdateChild, e.Index);
        }

        #endregion
    }
}
